const Sequelize = require('sequelize');
const Op = Sequelize.Op;
const folder = require('../models/model/folder');
const file = require('../models/model/file');


/**
 * 分页查询文件夹列表
 * @param {第几页} page 
 * @param {页容量} limit 
 * @param {模糊查询字段} search
 * @param {用户id} userId 
 */
exports.findBackstageFolder = async (page = 1, limit = 10, search = '', userId = '') => {
    let where;
    if (search == "") {
        where = {
            userId,
        }
    } else {
        where = {
            userId,
            name: {
                [Op.like]: '%' + search + '%',
            }
        }
    }
    const ins = await folder.findAll({
        offset: (page - 1) * limit,
        limit: +limit,
        where: where
    })
    if (ins) {
        return ins;
    }
    return null;
}

/**
 * 查询文件夹不分页
 * @param {模糊查询} search 
 * @param {用户id} userId 
 */
exports.findBackstageFolderAll = async (search = '', userId = '') => {
    let where;
    if (search == "") {
        where = {
            userId,
        }
    } else {
        where = {
            userId,
            name: {
                [Op.like]: '%' + search + '%',
            }
        }
    }
    const ins = await folder.findAll({
        where: where
    })
    if (ins) {
        return ins;
    }
    return null;
}

/**
 * 改变文件夹的权限
 * @param {是否可删除} isDelete 
 * @param {用户id} userId 
 * @param {文件夹id} id
 */
exports.changeBackstageFolderIsDelete = async (isDelete = false, userId = '', id = '') => {
    const ins = await folder.update(
        {
            isDelete,
        },
        {
            where: {
                userId,
                id
            }
        }
    )
    if (ins) {
        return ins;
    }
    return null;
}


/**
 * 通过文件夹id查询文件可分页，可模糊查询
 * @param {第几页} page 
 * @param {页容量} limit 
 * @param {模糊查询字段} search
 * @param {用户id} folderId 
 */
exports.findBackstageFile = async (page = 1, limit = 10, search = '', folderId = '') => {
    let where;
    if (search == "") {
        where = {
            folderId,
        }
    } else {
        where = {
            folderId,
            name: {
                [Op.like]: '%' + search + '%',
            }
        }
    }
    const ins = await file.findAll({
        offset: (page - 1) * limit,
        limit: +limit,
        where: where
    })
    if (ins) {
        return ins;
    }
    return null;
}

/**
 * 通过文件夹id查询文件
 * @param {模糊查询字段} search
 * @param {用户id} folderId 
 */
exports.findBackstageFileAll = async (search = '', folderId = '') => {
    let where;
    if (search == "") {
        where = {
            folderId,
        }
    } else {
        where = {
            folderId,
            name: {
                [Op.like]: '%' + search + '%',
            }
        }
    }
    const ins = await file.findAll({
        where: where
    })
    if (ins) {
        return ins;
    }
    return null;
}

/**
 * 设置文件的权限
 * @param {是否可删除} isDelete true不可删除 false可删除
 * @param {文件id} fileId 
 */
exports.changeBackstageFileIsDelete = async (isDelete, fileId, folderId) => {
    const ins = await file.update(
        {
            isDelete,
        },
        {
            where: {
                id: fileId,
                folderId
            }
        }
    )
    if (ins) {
        return ins;
    }
    return null;
}

/**
 * 删除单个文件
 * @param {文件id} fileId 
 * @param {文件夹id} folderId 
 */
exports.deleteBackstageFile = async (fileId, folderId) => {
    const ins = await file.destroy(
        {
            where: {
                id: fileId,
                folderId
            }
        }
    )
    if (ins) {
        return ins;
    }
    return null;
}

/**
 * 删除多个文件
 * @param {文件id数组} fileIdArr 
 * @param {文件夹id} folderId 
 */
exports.deleteBackstageFileArr = async (fileIdArr, folderId) => {
    const ins = await file.destroy({
        where: {
            folderId,
            id: {
                [Op.or]: fileIdArr,
            }
        }
    })
    if (ins) {
        return ins;
    }
    return null;
}

/**
 * 删除单个文件夹
 * @param {文件夹id} folderId 
 * @param {用户id} userId 
 */
exports.deleteBackstageFolder = async (folderId, userId) => {
    const ins = await folder.destroy(
        {
            where: {
                id: folderId,
                userId
            }
        }
    )
    if (ins) {
        return ins;
    }
    return null;
}

/**
 * 删除多个文件夹
 * @param {文件夹id数组} folderIdArr 
 * @param {用户id} userId 
 */
exports.deleteBackstageFolderArr = async (folderIdArr, userId) => {
    const ins = await folder.destroy({
        where: {
            userId,
            id: {
                [Op.or]: folderIdArr,
            }
        }
    })
    if (ins) {
        return ins;
    }
    return null;
}

/**
 * 通过文件夹id删除,主要是删除删除文件夹的同时删除文件
 * @param folderId 文件夹id
 */
exports.deleteFileFolder = async (folderId) => {
    const ins = await file.destroy({
        where:{
            folderId:folderId
        }
    })
    if(ins){
        return ins;
    }
    return null;
}

/**
 * 通过多个文件夹id删除文件,主要用于删除多个文件夹的时候删除同时删除文件
 * @param {*} folderIdArr 
 */
exports.deleteFileFolderArr = async (folderIdArr)=>{
    const ins = await file.destroy({
        where:{
            folderId:{
                [Op.or]: folderIdArr,
            }
        }
    })
    if(ins){
        return ins;
    }
    return null;
}